﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForumServer;
using ForumLibrary;
using ForumLibrary.Data;

namespace ForumGUI
{
    public partial class ViewEditPost : Form
    {
        private ClientController    _controller;
        private ForumManager       _manager;
        private String              _username;
        private String              _forum;
        private String              _thread;
        private String              _post; 
        private bool                _isView;
        public ViewEditPost(ClientController controller, ref ForumManager manager, String username, String forum, String thread,  String header, String content, bool isView)
        {
            InitializeComponent();
            _controller = controller;
            // push form
            _controller.pushForm(this);
            _manager = manager;
            _forum = forum;
            headerTextBox.Text = _post = header;
            contentTextBox.Text = content;
            _username = username;
            _thread = thread;
            if ((_isView = isView) == false) this.confirmButton.Text = "Edit";
            else
            {
                this.confirmButton.Visible = false;
                this.cancelButton.Visible = false;
                this.button1.Visible = true;
            }
            
            String toParse = "";
            try
            {
                toParse = _manager.getPostDetails(forum, thread, header);
            }
            catch
            {
                _controller.Logger.Error("Could not get Post Details");
            }
            String[] parsed = toParse.Split('~');
            informationLabel.Text = "Created by: " + parsed[2] + '\n';
            informationLabel.Text += "at: " + parsed[3] ;
        }
        

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Dispose(false);
            PostsFrame p = (PostsFrame)_controller.popForm();
            p.refreshPostList();
            p.Visible = true;
        }


        private void confirmButton_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(headerTextBox.Text))
            {
                MessageBox.Show("Please enter header");
                return;
            }
            if (String.IsNullOrEmpty(contentTextBox.Text))
            {
                MessageBox.Show("Please enter content");
                return;
            }
            String res = "";
            try
            {
                res = _manager.editPost(_username, _forum, _thread, _post, headerTextBox.Text, contentTextBox.Text);
            }
            catch
            {
                _controller.Logger.Error("Could not get Post Details");
            }
            if (res.Contains("successfully"))
            {
                MessageBox.Show(res);
                this.Dispose(false);
                PostsFrame pf = (PostsFrame)_controller.popForm();
                pf.refreshPostList();
                pf.Visible = true;

            }
            else
                MessageBox.Show(res, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        }

        private void contentLabel_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Dispose(false);
            PostsFrame p = (PostsFrame)_controller.popForm();
            p.refreshPostList();
            p.Visible = true;
        }

    }
}
